package cn.zjj.controller;

import cn.zjj.domain.User;
import cn.zjj.downgrade.DowngradeUtils;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
    @Value("${dynamicRefreshTest}")
    private String address;

    @SentinelResource(value = "user-getById",blockHandler = "blockMethod",blockHandlerClass = DowngradeUtils.class)
    @GetMapping("/getById/{id}")
    public User getById(@PathVariable("id") Long id){
        return new User(id,"Hell","废土:"+address);
    }

    // 限流与阻塞处理: 参数和被降级的方法参数一样,多个阻塞异常
/*    public User blockMethod(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();
        return new User(-1L,"超出最大允许流量","被限流");
    }*/
}
